package setting

import (
	"github.com/go-ini/ini"
	"log"
	"time"
)

var (
	Cfg *ini.File
	RunMode string

	HTTPPort int
	ReadTimeout time.Duration
	WriteTimeout time.Duration

	PageSize int
	JwtSecret string
)

func init()  {
	//Cfg
	var err error
	Cfg,err	= ini.Load("conf/app.ini")
	if err != nil {
		log.Fatalf("ini file err: %v",err)
	}
	//创建加载方式
	loadBase()
	//加载服务器配置
	loadServer()
	//加载app
	loadApp()
}

func loadBase(){
	RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug")
}

func loadServer()  {
	sec, err := Cfg.GetSection("server")
	if err != nil {
		log.Fatalf("获取server section err:%v",err)
	}
	HTTPPort = sec.Key("HTTP_PORT").MustInt(8000)
	ReadTimeout = time.Duration(sec.Key("READ_TIMEOUT").MustInt(60)) * time.Second
	WriteTimeout = time.Duration(sec.Key("READ_TIMEOUT").MustInt(60)) * time.Second


}


func loadApp(){
	app,err := Cfg.GetSection("app")
	if err != nil {
		log.Fatalf("获取app section err:%v",err)
	}
	PageSize = app.Key("PAGE_SIZE").MustInt(10)
	JwtSecret = app.Key("JWT_SECRET").MustString("!@)*#)!@U#@*!@!)")
}